package com.atguigu01._string.ex2;

import org.junit.Test;

public class StringTest {
    @Test
    public void t(){
        String str="abkkcadkabkebfkabkskab";
        String substr="kskab";
        System.out.println(Count(str,substr));
    }
/*
*题目3:获取一个字符串在另一个字符串中出现的次数。
比如:获取"ab"在"abkkcadkabkebfkabkskab”中出现的次数**/

    public int Count(String str,String substr){

        char[] arr1=str.toCharArray();
        char[] arr2=substr.toCharArray();
        if(arr1.length==0||arr2.length==0||arr1.length<arr2.length){
            return 0;
        }
        int i=0;
        int count=0;
        while(i<arr1.length-arr2.length+1){
            int ii=i;
            int m=0;
            for (int j = 0; j <arr2.length ; j++) {
                if (arr1[ii]==arr2[j]) {
                    m++;
                }
                ii++;
                if(m==arr2.length){
                    count++;
                }
            }
            i++;
        }
        return count;
    }


}
